import request from "@/utils/request.ts";
import {
  PermissionResponse,
  RoleListItem,
  RoleListParams,
  RoleListResponse,
} from "@/api/acl/role/type.ts";

enum API {
  // /admin/acl/role/{page}/{limit} GET 查询角色分页列表
  GET_ROLE_LIST = "/admin/acl/role",
  // /admin/acl/role/save POST 添加角色
  ROLE_SAVE = "/admin/acl/role/save",
  // /admin/acl/role/update put 修改角色
  ROLE_UPDATE = "/admin/acl/role/update",
  // /admin/acl/permission/toAssign/{roleId} GET 根据角色获取菜单
  GET_ROLE_MENU = "/admin/acl/permission/toAssign",
  // POST /admin/acl/permission/doAssign 分配权限
  ROLE_PERMISSION = "/admin/acl/permission/doAssign",
  // /admin/acl/role/batchRemove delete 批量删除角色
  ROLE_BATCH_REMOVE = "/admin/acl/role/batchRemove",
}

// 查询角色分页列表
export const reqRoleList = (params: RoleListParams) =>
  request.get<any, RoleListResponse>(
    API.GET_ROLE_LIST +
      `/${params.pageNo}/${params.pageSize}/?roleName=${params.roleName}`,
  );

// 新增角色
export const reqRoleSave = (data: RoleListItem) =>
  request.post<any, any>(API.ROLE_SAVE, data);

// 修改角色
export const reqRoleUpdate = (data: RoleListItem) =>
  request.put<any, any>(API.ROLE_UPDATE, data);

// 根据角色获取菜单
export const reqRoleMenu = (roleId: number) =>
  request.get<any, PermissionResponse>(API.GET_ROLE_MENU + `/${roleId}`);

// 分配权限
export const reqRolePermission = (roleId: number, menuIdList: number[]) =>
  request.post<any, any>(
    API.ROLE_PERMISSION + `/?roleId=${roleId}&permissionId=${menuIdList}`,
  );

// 批量删除角色
export const reqDeleteRoles = (idList: number[]) =>
  request.delete(API.ROLE_BATCH_REMOVE, {
    data: idList,
  });
